//
//  HomePageViewController.m
//  ShowYork
//
//  Created by hxh on 2020/8/24.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "HomePageMomentListViewController.h"
#import "HomePageDetailViewController.h"
#import "HomePageTopicListViewController.h"
#import "HomePageUserInfoViewController.h"
#import "DynamicPostViewController.h"
#import "HomePageContentViewController.h"
#import "HomePageMainListViewController.h"
#import "HomePageItemModel.h"
#import "MineFollowItemModel.h"
@interface HomePageMomentListViewController ()<IGListAdapterDataSource,UIScrollViewDelegate>

@property (nonatomic, copy) void(^scrollCallback)(UIScrollView *scrollView);

@property (nonatomic,strong)IGListAdapter *adapter;

@property (nonatomic,strong)IGListCollectionView *collectionView;

@property (nonatomic,strong)NSMutableArray *dataArray;

@property (nonatomic,strong)NSMutableArray *systemDataArray;

@property (nonatomic,assign)NSInteger page;


@property (nonatomic,strong)UIView *noDataView;

@property (nonatomic,strong)UIImageView *noDataImgView;

@property (nonatomic,strong)UILabel *noDateMainLb;
@property (nonatomic,strong)UILabel *noDataSubLb;

@property (nonatomic,strong)UIButton *noDataBtn;

@property (nonatomic,strong)UIButton *loginBtn;

@property (nonatomic,strong)UIButton *addBtn; ///发布按钮

/// 新手引导视图
@property (nonatomic,strong)UIView *helpCoverView; /// 新手引导遮盖层
@property (nonatomic,strong)UIImageView *helpNextImgView; /// 新手引导下一步引导图
@property (nonatomic,strong)UIImageView *helpRemoveImgView; /// 新手引导删除引导图

@property (nonatomic,assign)BOOL isFollowed;
@property (nonatomic,assign)BOOL isCurrentView;



@end

@implementation HomePageMomentListViewController


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (IGListCollectionView *)collectionView{
    if (!_collectionView) {
        _collectionView = [[IGListCollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH  - kNavBarHeight)];
        _collectionView.backgroundColor = RGBA(248, 248, 248, 1);
        _collectionView.showsVerticalScrollIndicator = NO;
        [self.view addSubview:_collectionView];
    }
    return _collectionView;
}
- (NSMutableArray *)systemDataArray{
    if (!_systemDataArray) {
        _systemDataArray = [NSMutableArray array];
    }
    return _systemDataArray;
}

- (UIButton *)addBtn{
    if (!_addBtn) {
        _addBtn = [[UIButton alloc]initWithFrame:CGRectZero];
        [_addBtn setBackgroundImage:[UIImage imageNamed:@"home_add_tag"] forState:UIControlStateNormal];
        [_addBtn setBackgroundImage:[UIImage imageNamed:@"home_add_tag"] forState:UIControlStateHighlighted];
        [_addBtn addTarget:self action:@selector(addMethod) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addBtn;
}

- (IGListAdapter *)adapter {
    if (!_adapter) {
        _adapter = [[IGListAdapter alloc] initWithUpdater:[[IGListAdapterUpdater alloc] init] viewController:self];
    }
    return _adapter;
    
}



- (void)reloadDataWithDeleteItem:(HomePageItemModel *)item{
    __block NSInteger index = -1;
    [self.dataArray enumerateObjectsUsingBlock:^(HomePageItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj.postId isEqualToNumber:item.postId]) {
            index = idx;
            *stop = YES;
        }
    }];
    if (index != -1) {
        [self.dataArray removeObjectAtIndex:index];
        [self.adapter reloadDataWithCompletion:nil];
    }
}
- (void)reloadItemFollowType:(NSNumber *)followType userId:(NSNumber *)userId{
    [self.dataArray enumerateObjectsUsingBlock:^(HomePageItemModel *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj.userId isEqualToNumber:userId]) {
            obj.followType = followType;
        }
    }];
    [self.adapter reloadDataWithCompletion:nil];
}

- (void)reloadData{
    [self.adapter reloadDataWithCompletion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [MobClick event:@"main_circle_show"];
    
    self.page = 1;
    
    [self setUI];
//
    if (![RPAppManager isLogin]) {
        self.noDataView.hidden = NO;
        self.loginBtn.hidden = NO;
        self.noDateMainLb.text = @"登录后可查看您的好友圈子";
        self.noDataSubLb.text = @"";
        self.noDataImgView.image = [UIImage imageNamed:@"noData"];
    }else{
        self.loginBtn.hidden = YES;

        [self showDataLoadState];

        // 先去请求是否有好友
        [self loadListDataSource];
    }
    self.isHideNavBar = YES;

    [self registerNote];
    
}

- (void)registerNote{
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateHomePageScrollViewContent:) name:@"updateHomePageScrollViewContent" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deleteHomePageItem:) name:@"deleteHomePageItem" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateHomePageList) name:@"updateUserInfoIcon" object:nil];
    // 登录完刷新页面
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(LoginSuccess) name:@"updateLoginUserInfoFollowListStatus" object:nil];
    // 发布完动态刷新页面
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshData) name:@"updateUserInfoList" object:nil];
  
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateItemListStatusSource:) name:@"updateItemListSource" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateFollowListStatus:) name:@"updateUserInfoFollowListStatus" object:nil];
    
}

#pragma mark - 处理通知
- (void)updateFollowListStatus:(NSNotification *)note{
    
    if (self.dataArray.count == 0) {
        self.page = 1;
        [self loadListDataSource];
    }else{
        MineFollowItemModel *itemModel = note.userInfo[@"data"];
        [self.dataArray enumerateObjectsUsingBlock:^(HomePageItemModel *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj.userId isEqualToNumber:itemModel.userId]) {
                obj.followType = itemModel.followCode;
            }
        }];
        [self.adapter reloadDataWithCompletion:nil];
    }
}

- (void)updateHomePageScrollViewContent:(NSNotification *)note{
  
    self.isCurrentView = [note.userInfo[@"status"] boolValue];
    
    // 判读登录用户关注用户为空，并且登录，点击当前页面，新用户发送了动态
    if ([RPAppManager isLogin] && self.isCurrentView) {
        if (self.dataArray.count == 0) {
            [self refreshData];
        }else{
            [self setFirstHelpView];
        }
    }
}
- (void)LoginSuccess{
    
    self.loginBtn.hidden = YES;
    
    self.page = 1;
    
    [self showDataLoadState];
    
    [self loadListDataSource];
    
}

- (void)refreshData{
    
    self.page = 1;
    
    [self showDataLoadState];
    
    [self loadListDataSource];
    
}

- (void)updateItemListStatusSource:(NSNotification *)note{
   
    if (self.dataArray.count == 0) {
        self.page = 1;
        [self loadListDataSource];
    }else{
        HomePageItemModel *itemModel = note.userInfo[@"data"];
        [self.dataArray enumerateObjectsUsingBlock:^(HomePageItemModel *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj.userId isEqualToNumber:itemModel.userId]) {
                obj.followType = itemModel.followType;
            }
        }];
        [self.adapter reloadDataWithCompletion:nil];
    }
}
/// 其他页面删除动态
- (void)deleteHomePageItem:(NSNotification *)note{
    __block NSInteger index = -1;
    HomePageItemModel *itemModel = note.userInfo[@"data"];
    [self.dataArray enumerateObjectsUsingBlock:^(HomePageItemModel *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj.postId isEqualToNumber:itemModel.postId]) {
            index = idx;
        }
    }];
    if (index != -1) {
        [self.dataArray removeObjectAtIndex:index];
        [self.adapter reloadDataWithCompletion:nil];
    }
}
// 更新用户头像
- (void)updateHomePageList{
    [self.adapter reloadDataWithCompletion:nil];
}

- (void)setUI{
    // 主列表
    self.adapter.dataSource = self;
    self.adapter.collectionView = self.collectionView;
    self.adapter.scrollViewDelegate = self;
    __weak typeof(self)weakSelf = self;
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.page = 1;
        [weakSelf loadListDataSource];
      
    }];
    self.collectionView.mj_header = header;
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
      
        [MobClick event:@"homePage_footer_refresh"];
        
        weakSelf.page++;
        [weakSelf loadUserListSource];
     
    }];
    [footer setTitle:@"—— 没有更多了 ——" forState:MJRefreshStateNoMoreData];
    footer.stateLabel.font = font(10);
    footer.stateLabel.textColor = RGBA(153, 153, 153, 1);
    footer.hidden = YES;
    self.collectionView.mj_footer = footer;
    
    self.noDataView = [[UIView alloc]init];
    self.noDataView.backgroundColor = [UIColor whiteColor];
    self.noDataView.hidden = YES;
    [self.view addSubview:self.noDataView];
    
    [self.noDataView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];
   
    self.noDataImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mine_noList_tag"]];
    [self.noDataView addSubview:self.noDataImgView];
    
    [self.noDataImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.view).offset(-kNavBarHeight);
        make.centerX.equalTo(self.view);
    }];
    
    self.noDateMainLb = [[UILabel alloc]init];
    self.noDateMainLb.text = @"无内容";
    self.noDateMainLb.textColor = C153;
    self.noDateMainLb.font = font(12);
    self.noDateMainLb.textAlignment = NSTextAlignmentCenter;
    [self.noDataView addSubview:self.noDateMainLb];
    [self.noDateMainLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.noDataView);
        make.top.equalTo(self.noDataImgView.mas_bottom).offset(8);
    }];
    self.noDataSubLb = [[UILabel alloc]init];
    self.noDataSubLb.text = @"无内容";
    self.noDataSubLb.textColor = C153;
    self.noDataSubLb.font = font(12);
    self.noDataSubLb.textAlignment = NSTextAlignmentCenter;
    [self.noDataView addSubview:self.noDataSubLb];
    [self.noDataSubLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.noDateMainLb);
        make.top.equalTo(self.noDateMainLb.mas_bottom).offset(2);
    }];
    
    self.loginBtn = [[UIButton alloc]init];
    [self.loginBtn setBackgroundImage:[UIImage imageNamed:@"home_topic_post_tag"] forState:UIControlStateNormal];
    [self.loginBtn setBackgroundImage:[UIImage imageNamed:@"home_topic_post_tag"] forState:UIControlStateHighlighted];
    [self.loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [self.loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.loginBtn addTarget:self action:@selector(loginMethod) forControlEvents:UIControlEventTouchUpInside];
    self.loginBtn.titleLabel.font = font(14);
    self.loginBtn.hidden = YES;
    [self.view addSubview:self.loginBtn];
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.noDataSubLb.mas_bottom).offset(55);
        make.centerX.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(90, 34));
    }];
    
    self.addBtn.hidden = YES;
    [self.view addSubview:self.addBtn];

    [self.addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(-12);
        make.bottom.equalTo(self.view).offset(-56);
        make.size.mas_equalTo(CGSizeMake(50, 50));
    }];
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, -self.collectionView.cl_height, kScreenW, self.collectionView.cl_height)];
    view.layer.zPosition = -1;
    view.backgroundColor = UIColor.whiteColor;
    [self.collectionView addSubview:view];
    
}


- (void)setFirstHelpView{
    

    BOOL isFirstHomePageUse = [[[NSUserDefaults standardUserDefaults] objectForKey:@"isFirstHomePageUse"] boolValue];
    
    if (!isFirstHomePageUse &&
        [RPAppManager isLogin] &&
        self.isCurrentView &&
        [[RPAppManager jsd_findVisibleViewController] isKindOfClass:[HomePageMainListViewController class]]
        ) {
  
        self.helpCoverView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
        self.helpCoverView.backgroundColor = RGBA(0, 0, 0, 0.7);
        
        [[UIApplication sharedApplication].keyWindow addSubview:self.helpCoverView];
        
        self.helpNextImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"common_next_tag"]];
        [self.helpCoverView addSubview:self.helpNextImgView];

        [self.helpNextImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.helpCoverView).offset(-70);
            make.bottom.equalTo(self.helpCoverView).offset(-(56 + 16));
        }];
        self.helpRemoveImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"common_help_tag"]];
        [self.helpCoverView addSubview:self.helpRemoveImgView];
        [self.helpRemoveImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.helpCoverView).offset(-(56 + 44 + 44));
            make.right.equalTo(self.helpCoverView).offset(-16);
        }];

        self.helpRemoveImgView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removeHelpView)];
        [self.helpRemoveImgView addGestureRecognizer:tap];
        
        
        UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(0,0,kScreenW,kScreenH)];
        // 透明path
        UIBezierPath *path2 = [[UIBezierPath bezierPathWithRoundedRect:CGRectMake(kScreenW - 12 - 50,kScreenH - (56 + 50),50, 50) cornerRadius:25] bezierPathByReversingPath];
        [path appendPath:path2];
        // 绘制透明区域
        CAShapeLayer *shapeLayer = [CAShapeLayer layer];
        shapeLayer.path = path.CGPath;
        [self.helpCoverView.layer setMask:shapeLayer];
    }
}

- (void)addMethod{
    [MobClick event:@"main_circle_add_click"];
    
    if (![RPAppManager isLogin]) {
        [MGJRouter openURL:@"local://ddup/unLogin" withUserInfo:@{@"navigationVC":self.navigationController} completion:nil];
        return;
    }
    DynamicPostViewController *vc = [[DynamicPostViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)loginMethod{
    [MGJRouter openURL:@"local://ddup/unLogin" withUserInfo:@{@"navigationVC":self.navigationController} completion:nil];
}

- (void)removeAllHelpView{
    [self.helpCoverView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.helpCoverView removeFromSuperview];
    
    self.helpCoverView = nil;
    self.helpNextImgView = nil;
    self.helpRemoveImgView = nil;
}

- (void)removeHelpView{
    [self.helpCoverView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.helpCoverView removeFromSuperview];
    [[NSUserDefaults standardUserDefaults] setObject:@1 forKey:@"isFirstHomePageUse"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    self.helpCoverView = nil;
    self.helpNextImgView = nil;
    self.helpRemoveImgView = nil;
}




- (void)loadListDataSource{
    
    if (!self.isCurrentView) {
        return;
    }
    
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self loadUserFollowListSource];
    });
    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self loadSystemListSource];
    });
    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self loadUserListSource];
    });
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
       
        [self hideDataLoadLoadState];
        [self.systemDataArray enumerateObjectsUsingBlock:^(HomePageItemModel *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            __block NSInteger index = -1;
            [self.dataArray enumerateObjectsUsingBlock:^(HomePageItemModel *_Nonnull subObject, NSUInteger subIndex, BOOL * _Nonnull stop) {
                if([subObject.postId isEqualToNumber:obj.postId]){
                    index = subIndex;
                    *stop = YES;
                }
            }];
            
            if (index != -1) {
                [self.dataArray removeObjectAtIndex:index];
            }
            [self.dataArray insertObject:obj atIndex:idx];
        }];
      

        
//
        [self.adapter performUpdatesAnimated:NO completion:^(BOOL finished) {
            if (finished) {
                self.collectionView.mj_footer.hidden = self.dataArray.count == 0;
                self.noDataView.hidden = self.dataArray.count > 0;

                // 有关注用户
                if (self.isFollowed && self.dataArray.count == 0) {
                    self.noDateMainLb.text = @"暂无好友发布动态…";
                    self.noDataSubLb.text = @"快点击右下角「发布」动态抢沙发吧～";
                    self.noDataImgView.image = [UIImage imageNamed:@"mine_noList_tag"];
                }

                if (!self.isFollowed && self.dataArray.count == 0) {
                    self.noDataView.hidden = NO;
                    self.noDateMainLb.text = @"您还没有关注的好友哦～";
                    self.noDataSubLb.text = @"快到「广场」频道寻找志同道合的朋友并关注TA吧～";
                    self.noDataImgView.image = [UIImage imageNamed:@"mine_noData_tag"];
                }
                self.addBtn.hidden = NO;
                [self setFirstHelpView];
            }
        }];
        
    });
}

- (void)loadUserFollowListSource{
    dispatch_semaphore_t sema = dispatch_semaphore_create(0);

    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"userId"] = [RPAppManager getCustomerId];
    param[@"md5"] = [RPAppManager md5StrWithApi:UserFollow];
    param[@"pageSize"] = @(10);
    param[@"pageIndex"] = @(1);
    [[RPNetworkExtensionTool sharedTool] requestPOST:kGET_REQUEST_URL(UserFollow) parames:param success:^(id responseObj) {
        
        if ([responseObj[RESPONSE][LIST] isKindOfClass:[NSArray class]]) {
            NSArray *arr = responseObj[RESPONSE][LIST];
            self.isFollowed = arr.count > 0;
        }else{
            self.isFollowed = NO;
        }
        dispatch_semaphore_signal(sema);
    } failure:^(id error) {
        dispatch_semaphore_signal(sema);
    }];
    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}


- (void)loadSystemListSource{
    dispatch_semaphore_t sema = dispatch_semaphore_create(0);
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"userId"] = [RPAppManager getCustomerId];
    param[@"md5"] =  [RPAppManager md5StrWithApi:GetOfficialPostDetailList];
    param[@"type"] = @"1";
    [[RPNetworkExtensionTool sharedTool]requestPOST:kGET_REQUEST_URL(GetOfficialPostDetailList) parames:param success:^(id responseObj) {
        self.systemDataArray = [HomePageItemModel mj_objectArrayWithKeyValuesArray:responseObj[RESPONSE]];
        dispatch_semaphore_signal(sema);
    } failure:^(id error) {
        dispatch_semaphore_signal(sema);
    }];
    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
    
}

- (void)loadUserListSource{
    dispatch_semaphore_t sema = dispatch_semaphore_create(0);
    if (self.page > 1) {
        sema = dispatch_semaphore_create(1);
    }

    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"userId"] = [RPAppManager getCustomerId];
    param[@"pageIndex"] = @(self.page);
    param[@"pageSize"] = @(10);
    param[@"type"] = @"1";
    param[@"md5"] = [RPAppManager md5StrWithApi:GetIndexPostList];
    [[RPNetworkExtensionTool sharedTool] requestPOST:kGET_REQUEST_URL(GetIndexPostList) parames:param success:^(id responseObj) {
        if ([self.collectionView.mj_footer isRefreshing]) {
            [self.collectionView.mj_footer endRefreshing];
        }
        if ([self.collectionView.mj_header isRefreshing]) {
            [self.collectionView.mj_header endRefreshing];
        }
        if (self.page == 1) {
            self.dataArray = [HomePageItemModel mj_objectArrayWithKeyValuesArray:responseObj[RESPONSE][LIST]];
        }else{
            [self.dataArray addObjectsFromArray:[HomePageItemModel mj_objectArrayWithKeyValuesArray:responseObj[RESPONSE][LIST]]];
            
        }
        CGFloat bottom = self.collectionView.mj_insetB;
        if ([responseObj[RESPONSE][LASTPAGE] boolValue] || ![responseObj[RESPONSE][NEXTPAGE]boolValue]) {
            [self.collectionView.mj_footer endRefreshingWithNoMoreData];
            if (self.collectionView.mj_footer.cl_height == 44.0f) {
                self.collectionView.mj_footer.cl_height = 138.0f;
                if (bottom == 44.0f || bottom == GK_SAVEAREA_BTM + 44.0f) {
                      self.collectionView.mj_insetB = 138.0f;
                }
            }
        }else{
            [self.collectionView.mj_footer resetNoMoreData];
            if (self.collectionView.mj_footer.cl_height == 138.0f) {
                self.collectionView.mj_footer.cl_height = 44.0f;
                if (bottom == 138.0f) {
                    self.collectionView.mj_insetB = 44.0f;
                }
            }
        }
        
        if (self.page > 1) {
            [self.adapter performUpdatesAnimated:NO completion:nil];
        }
        
        dispatch_semaphore_signal(sema);
    } failure:^(id error) {
        self.page--;
        dispatch_semaphore_signal(sema);
    }];
    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}

#pragma mark - IGListAdapterDataSource
- (NSArray<id <IGListDiffable>> *)objectsForListAdapter:(IGListAdapter *)listAdapter{
    return self.dataArray;
}

- (IGListSectionController *)listAdapter:(IGListAdapter *)listAdapter sectionControllerForObject:(id)object{
    IGListSectionController *vc = nil;
    if ([object isKindOfClass:[HomePageItemModel class]]){
 
        HomePageItemModel *firstModel = self.dataArray.firstObject;
        firstModel.showLine = NO;
        
        vc = [[HomePageContentViewController alloc]init];
        ((HomePageContentViewController *)vc).model = object;
        // 查看头像
        __weak typeof(self)weakSelf = self;
        ((HomePageContentViewController *)vc).showUserInfo = ^(NSNumber * _Nonnull userId) {
            
            [MobClick event:@"homePage_show_userInfo"];
            
            HomePageUserInfoViewController *userInfoVC = [[HomePageUserInfoViewController alloc]initWithUserId:userId];
            [weakSelf.navigationController pushViewController:userInfoVC animated:YES];
        };
        ((HomePageContentViewController *)vc).showDetail = ^(HomePageItemModel * _Nonnull currentModel,BOOL isComment) {
            NSInteger index = [self.dataArray indexOfObject:currentModel];
            XHPageDetailPlatformType platformType = XHPageDetailPlatformTypeNormal;
            if (isComment) {
                // 弹出输入框
                if (currentModel.commentNum.intValue == 0) {
                    platformType = XHPageDetailPlatformTypeBeginText;
                // 弹出评论框
                }else{
                    platformType = XHPageDetailPlatformTypeShowComment;
                }
            }
            
            NSMutableDictionary *param = [NSMutableDictionary dictionary];
            param[kXHItemType] = @(XHPageDetailItemTypeNormal);
            param[kXHPlatformType] = @(platformType);
            param[kXHShowUserInfo] = @(YES);
            param[kXHListSource] = self.dataArray;
            param[kXHCurrentIndex] = @(index);
            param[kXHCurrentPage] = @(self.page);
            param[kXHHeaderHidden] = @(NO);
            param[kXHUserCreateTime] = @(YES);
            HomePageDetailViewController *dtlVC = [[HomePageDetailViewController alloc]initWithConfigInfo:param];
            [weakSelf.navigationController pushViewController:dtlVC animated:YES];
        };

        
        ((HomePageContentViewController *)vc).showTopicList = ^(HomeTagItemModel * _Nonnull currentModel) {
            HomePageTopicListViewController *listVC = [[HomePageTopicListViewController alloc]init];
            listVC.tagModel = currentModel;
            [weakSelf.navigationController pushViewController:listVC animated:YES];
        };
    }
   
    return vc;
}

- (void)updateItemWithCurrentModel:(HomePageItemModel *)itemModel{
    [self.adapter reloadDataWithCompletion:nil];
}

- (void)reloadTargetItemWithCurrentModel:(HomePageItemModel *)itemModel{
    [self updateItemWithCurrentModel:itemModel];
}

- (nullable UIView *)emptyViewForListAdapter:(IGListAdapter *)listAdapter{
    
    return [UIView new];
}


- (UIView *)listView {
    return self.view;
}

- (UIScrollView *)listScrollView {
    return self.collectionView;
}

- (void)listViewDidScrollCallback:(void (^)(UIScrollView *))callback {

    self.scrollCallback = callback;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    !self.scrollCallback ?: self.scrollCallback(scrollView);
}


@end
